<?php

namespace app\api\controller\customer;


use app\api\controller\Api;
use app\common\customer\model\Customer;
use app\common\customer\model\CustomerSource;
use app\common\system\logic\FormLogic;
use think\db\exception\DbException;

class Source extends Api
{
    public function append()
    {
        $post = $this->request->post();
        if (empty($post['member_id']) || empty($post['source_id'])) {
            return error([], "参数错误");
        }

        unset($post['id']);

        $model = new CustomerSource();
        $post['create_id'] = $this->request->user['id'];
        try {
            $model->save($post);
            return success([], "操作成功");
        } catch (DbException $e) {
            return error([], "操作失败");
        }
    }

    public function save()
    {
        $post = $this->request->post();
        if (empty($post['member_id']) || empty($post['source_id'])) {
            return error([], "参数错误");
        }

        $model = new Customer();
        $row = $model->where('id', $post['member_id'])->find();
        try {
            $row->save(['source_id' => $post['source_id']]);
        } catch (DbException $e) {
            return error([], $e->getMessage());
        }

        return success([], "操作成功");
    }
}
